﻿using DataModel.Contants;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace CommonLibrary.Helper
{
    public class DomainHelper
    {
        public static string WallpaperPath = string.Empty;

        public static string GetRootPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        /// <summary>
        /// 获取目录
        /// </summary>
        /// <param name="directory">一级目录</param>
        /// <param name="directory2">二级目录</param>
        /// <returns></returns>
        public static string GetDirectoryPath(string directory, string directory2)
        {
            string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directory, directory2);
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
            }
            Directory.CreateDirectory(dir);
            return dir;
        }

        /// <summary>
        /// 获取目录
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public static string GetDirectoryPath(string directory)
        {
            string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directory);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return dir;
        }

        /// <summary>
        /// 获取壁纸源地址
        /// </summary>
        /// <returns></returns>
        public static string GetWallpaperPath()
        {
            return string.IsNullOrEmpty(WallpaperPath) ? GetDirectoryPath(DirectoryDefine.WALLPAPERS) : WallpaperPath;
        }

        /// <summary>
        /// 获取壁纸详情目录（已过时）
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public static string GetWallpaperPath(string directory)
        {
            if (string.IsNullOrEmpty(WallpaperPath))
            {
                return GetDirectoryPath(DirectoryDefine.WALLPAPERS, directory);
            }
            else
            {
                var dir = Path.Combine(WallpaperPath, directory);
                if (Directory.Exists(dir))
                {
                    Directory.Delete(dir, true);
                }
                Directory.CreateDirectory(dir);
                return dir;
            }
        }

        /// <summary>
        /// 获取壁纸缓存
        /// </summary>
        /// <param name="url">服务器文件路径</param>
        /// <returns></returns>
        public static string GetCachedFilePath(string url)
        {
            string fileName = Convert.ToBase64String(Encoding.UTF8.GetBytes(url)).Replace("/", "_") + ".dat";
            string dir = GetWallpaperPath();
            return Path.Combine(dir, fileName);
        }

        /// <summary>
        /// 获取软件缓存
        /// </summary>
        /// <param name="cache"></param>
        /// <returns></returns>
        public static string GetCachePath(string cache)
        {
            string fileName = Convert.ToBase64String(Encoding.UTF8.GetBytes(cache)).Replace("/", "_") + ".dat";
            string dir = GetDirectoryPath("caches");
            return Path.Combine(dir, fileName);
        }
    }
}
